package com.zlzlib.liblayoutmanager;

import android.content.Context;
import android.view.View;

/**
 * @Desc: 滑动中心缩放的类型
 * @Copyright:
 * @DateTime: 2021/8/17 10:31
 * @Author zlz
 * @Version 1.0
 */
public class LayoutScaleManager extends BaseLayoutManager {

    //每一项的间隔
    protected int itemSpace;
    //缩放的最小值
    protected float minScale;
    //透明度
    private float maxAlpha;
    private float minAlpha;

    public LayoutScaleManager(Context context) {
        this(context,0);
    }

    public LayoutScaleManager(Context context, int itemSpace) {
        this(context,itemSpace,HORIZONTAL);
    }

    public LayoutScaleManager(Context context, int itemSpace, int orientation) {
        super(context, orientation, false);
        this.itemSpace = itemSpace;
        minScale = 0.8f;
        maxAlpha = 1f;
        minAlpha = 1f;
    }

    @Override
    protected float setInterval() {
        return itemSpace + mDecoratedMeasurement;
    }

    @Override
    protected void setItemViewProperty(View itemView, float targetOffset) {
        //计算缩放值
        float scale = calculateScale(targetOffset);
        itemView.setScaleX(scale);
        itemView.setScaleY(scale);
        //设置透明度
        final float alpha = calAlpha(targetOffset);
        itemView.setAlpha(alpha);
    }

    private float calculateScale(float x) {
        //当前item偏移距离绝对值
        float deltaX = Math.abs(x);
        //如果偏移距离超过item的大小 那么设偏移距离为item的大小
        if (deltaX - mDecoratedMeasurement > 0) deltaX = mDecoratedMeasurement;
        return 1f - deltaX / mDecoratedMeasurement * (1f - minScale);
    }

    private float calAlpha(float targetOffset) {
        //当前item偏移距离绝对值
        final float offset = Math.abs(targetOffset);
        float alpha = (minAlpha - maxAlpha) / mInterval * offset + maxAlpha;
        //如果偏移距离超过item的大小 设置为最小透明度
        if (offset >= mInterval) alpha = minAlpha;
        return alpha;
    }

    public int getItemSpace() {
        return itemSpace;
    }

    public void setItemSpace(int itemSpace) {
        assertNotInLayoutOrScroll(null);
        if (this.itemSpace == itemSpace) return;
        this.itemSpace = itemSpace;
        removeAllViews();
    }

    public float getMinScale() {
        return minScale;
    }

    public void setMinScale(float minScale) {
        assertNotInLayoutOrScroll(null);
        if (this.minScale == minScale) return;
        this.minScale = minScale;
        removeAllViews();
    }

    public float getMaxAlpha() {
        return maxAlpha;
    }

    public void setMaxAlpha(float maxAlpha) {
        assertNotInLayoutOrScroll(null);
        if (maxAlpha > 1) maxAlpha = 1;
        if (this.maxAlpha == maxAlpha) return;
        this.maxAlpha = maxAlpha;
        requestLayout();
    }

    public float getMinAlpha() {
        return minAlpha;
    }

    public void setMinAlpha(float minAlpha) {
        assertNotInLayoutOrScroll(null);
        if (minAlpha > 1) minAlpha = 1;
        if (this.minAlpha == minAlpha) return;
        this.minAlpha = minAlpha;
        requestLayout();
    }
}
